package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Notice;
import cn.wolfcode.mapper.NoticeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.INoticeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;


    @Override
    public void save(Notice notice,Long[] ids) {
        noticeMapper.insert(notice);
        if (ids != null&&ids.length>0) {
            for (Long id : ids) {
                noticeMapper.insertRelation(notice.getId(),id);
            }
        }
    }

    @Override
    public void delete(Long id) {
        noticeMapper.deleteByPrimaryKey(id);
        noticeMapper.deleteRelation(id);
    }

    @Override
    public void update(Notice notice,Long [] ids) {
        noticeMapper.deleteRelation(notice.getId());
        noticeMapper.updateByPrimaryKey(notice);
        if (ids != null&&ids.length>0) {
            for (Long id : ids) {
                noticeMapper.insertRelation(notice.getId(),id);
            }
        }
    }

    @Override
    public Notice get(Long id) {
        return noticeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Long> listAll() {
        return noticeMapper.selectAll();
    }

    @Override
    public PageInfo<Notice> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Notice> notices = noticeMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Notice>(notices);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        noticeMapper.updateStatus(id,status);
    }

    @Override
    public List<Notice> selectNoticeByEmpId(Long id) {
       return noticeMapper.selectNoticeByEmpId(id);
    }

    @Override
    public void updateReadr(Long id, Integer readr) {
        noticeMapper.updateReadr(id,readr);
    }

    @Override
    public int selectUnread(boolean readr) {
        return  noticeMapper.selectUnread(readr);

    }

    @Override
    public List<Long> selectNoticeCountByEmpId(Long id) {
        return noticeMapper.selectNoticeCountByEmpId(id);
    }

    @Override
    public List<Notice> selectNOLook(List<Long> list) {
        return noticeMapper.selectNOLook(list);
    }


}
